﻿using System;
using System.Collections.Generic;
using System.Diagnostics;

namespace SalesSys.Comm.Utility
{

    /// <summary>
    /// 简单日志输出，使用LogManager.GetLog获取实例
    /// </summary>
    public class FastTrace : TraceSource
    {

        internal FastTrace(string name) : base(name, SourceLevels.All) { }

        int GetNextId()
        {
            return LogManager.GetNextId();
        }
        /// <summary>
        /// 调试信息，Verbose
        /// args长度为0直接写入data，data为null则直接将args依次写入,data不为null,则当作格式化字符串处理即：string.Format(data.ToString(), args)
        /// </summary>
        /// <param name="data">数据信息或格式化信息string.Format(data.ToString(), args)</param>
        /// <param name="args">表示与data相关数据，data为null则直接将其写入,data不为null,则当作格式化字符串处理即：string.Format(data.ToString(), args)</param>
        [Conditional("DEBUG")]
        public void Debug(object data, params object[] args)
        {
            WriteData(TraceEventType.Verbose, data, args);
        }

        [Conditional("DEBUG")]
        public void d(object data, params object[] args)
        {
            Debug(data, args);
        }
        /// <summary>
        /// 普通信息，Information
        /// </summary>
        /// <param name="data"></param>
        /// <param name="args"></param>
        [Conditional("TRACE")]
        public void Info(object data, params object[] args)
        {
            WriteData(TraceEventType.Information, data, args);
        }
        [Conditional("TRACE")]
        public void i(object data, params object[] args)
        {
            Info(data, args);
        }
        /// <summary>
        /// 警告，Warning
        /// </summary>
        /// <param name="data"></param>
        /// <param name="args"></param>
        [Conditional("TRACE")]
        public void Warn(object data, params object[] args)
        {
            WriteData(TraceEventType.Warning, data, args);
        }
        [Conditional("TRACE")]
        public void w(object data, params object[] args)
        {
            Warn(data, args);
        }

        /// <summary>
        /// 错误
        /// </summary>
        /// <param name="data"></param>
        /// <param name="args"></param>
        [Conditional("TRACE")]
        public void Error(object data, params object[] args)
        {
            WriteData(TraceEventType.Error, data, args);
        }
        [Conditional("TRACE")]
        public void e(object data, params object[] args)
        {
            Error(data, args);
        }
        /// <summary>
        /// 程序崩溃型错误，最高级别错误
        /// args长度为0直接写入data，data为null则直接将args依次写入,data不为null,则当作格式化字符串处理即：string.Format(data.ToString(), args)
        /// </summary>
        /// <param name="data">数据信息或格式化信息string.Format(data.ToString(), args)</param>
        /// <param name="args">表示与data相关数据，data为null则直接将其写入,data不为null,则当作格式化字符串处理即：string.Format(data.ToString(), args)</param>
        [Conditional("TRACE")]
        public void Critical(object data, params object[] args)
        {
            WriteData(TraceEventType.Critical, data, args);
        }
        [Conditional("TRACE")]
        public void c(object data, params object[] args)
        {
            Critical(data, args);
        }
        [Conditional("TRACE")]
        void WriteData(TraceEventType eventType, object data, params object[] args)
        {
            //TraceEventCache
            var isArgs = args.Length > 0;
            if (data != null && isArgs)
                TraceData(eventType, GetNextId(), DateTime.Now.ToString(), string.Format(data.ToString(), args));
            else if (data == null && !isArgs)
                TraceData(eventType, GetNextId(), DateTime.Now.ToString(), string.Empty);
            else if (!isArgs)
            { 
                TraceData(eventType, GetNextId(), DateTime.Now.ToString(), data);
            }
            else if (data == null)
                TraceData(eventType, GetNextId(), DateTime.Now.ToString(), args);
        }

    }

    public class LogManager
    {
        static int index = 0;
        internal static int GetNextId()
        {
            lock (logs)
            {
                return ++index;
            }
        }
        static Dictionary<string, FastTrace> logs = new Dictionary<string, FastTrace>();
        static public FastTrace GetLog(string name)
        {
            if (logs.ContainsKey(name))
            {
                return logs[name];
            }
            else
            {
                var log = new FastTrace(name);
                logs[name] = log;
                return log;
            }
        }
        static FastTrace mainTrace;
        public static FastTrace m
        {
            get
            {
                if (mainTrace == null)
                {
                    mainTrace = GetLog("mainTrace");
                }
                return mainTrace;
            }
        }

    }
    /// <summary>
    /// 快速日志类，使用当前mainTrace日志源输出，此类主要便于快速书写
    /// </summary>
    public class Log
    {
        public static FastTrace m
        {
            get
            {
                return LogManager.m;
            }
        }
    }
}